﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TagLib;

using WMPLib;

using RedPanda.MusicPlayer.Utils;

namespace RedPanda.MusicPlayer.DataModel
{
    //表示音频文件的实体类;
    //可以序列化为单个XML文件
    [Serializable]
    public class AudioFile
    {
        #region Field
        //歌曲的id用来唯一标示一首歌曲;
        private string id; 
        private String location; // 音乐文件实际地址
        private String title;//歌曲名称
        private String artist;
        private String genre;
        private String year;
        private String album;
        private Double duration;

        //去掉歌词和评论，把歌词单独保存至一个文件夹中;
        //添加lyricFileName属性，在获得一首歌曲的歌词之后生成唯一的GUID并且记录下来，
        //用来标示该文件的歌词;
        //private String lyric;
        //private String comment;
        #endregion 

        #region Constructor
        public AudioFile()
        {
        }

        public AudioFile(string location)
        {
            this.id = Guid.NewGuid().ToString();
            this.location = location;

            //从文件中读入该音乐文件的属性值;
            File file = File.Create(location);
            Tag tag = file.Tag;

            //暂时设为一个定值;
            this.title = tag.Title;
            this.artist = GetFirstString(tag.Artists);
            this.album = tag.Album==null?"Unknow":tag.Album;
            this.year = tag.Year+"";
            this.genre = GetFirstString(tag.Genres);

            //获得音频文件的长度;
            //现在的方法不太可行，继续寻找解决方法;
            //this.duration = MusicPlayerUtils.GetAudioDuration(location);
            this.duration = 0;
        }

        #endregion


        private string GetFirstString(string[] strs)
        {
            if (strs == null || strs.Length == 0)
            {
                return "Unknow";
            }
            return strs[0];
        }

        #region Property
        public String Location
        {
            get { return location; }
            set { location = value; }
        }
        

        public String Title
        {
            get { return title; }
            set { title = value; }
        }
        

        public String Artist
        {
            get { return artist; }
            set { artist = value; }
        }
        

        public String Genre
        {
            get { return genre; }
            set { genre = value; }
        }
        

        public String Year
        {
            get { return year; }
            set { year = value; }
        }
        

        public String Album
        {
            get { return album; }
            set { album = value; }
        }
        

        public Double Duration
        {
            get { return duration; }
            set { duration = value; }
        }

        public string ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        #endregion
    }
}
